package com.binvshe.binvshe.app.home;

import android.os.Bundle;

import com.binvshe.binvshe.app.home.adapter.HomeAdapter;
import com.binvshe.binvshe.common.http.ListDataRequestListener;
import com.binvshe.binvshe.common.http.MyHttpRequest;
import com.binvshe.binvshe.common.http.MyResponseParser;
import com.binvshe.binvshe.common.http.UrlCenter;
import com.binvshe.binvshe.entity.HomeEntity;
import com.f2prateek.dart.Dart;
import com.f2prateek.dart.InjectExtra;
import com.pachong.android.baseuicomponent.adapter.RecyclerViewBaseAdapter;
import com.pachong.android.baseuicomponent.fragment.SwipeListFragment;
import com.pachong.android.framework.httprequest.IHttpRequest;
import com.pachong.android.framework.httprequest.UrlParams;

import java.util.List;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2017/1/22
 */

public class HomeListFragment extends SwipeListFragment {

    public static final String KEY_CHANNELID = "CHANNELID";
    private final static int PAGE_SIZE = 10;
    private int lastListSize = 0;
    int mlastid = 0;
    @InjectExtra(KEY_CHANNELID)
    int mChannelID = 0;


    MyResponseParser<HomeEntity> parser = new MyResponseParser<HomeEntity>(HomeEntity.class) {
        @Override
        public boolean hasMore() {
            if (lastListSize >= PAGE_SIZE) {
                return true;
            } else {
                return false;
            }
        }
    };

    ListDataRequestListener mRequestListener = new ListDataRequestListener(this, parser) {
        @Override
        public void onSuccess(Object listData) {
            super.onSuccess(listData);
            if (getData().size() > 0) {
                mlastid = ((HomeEntity) getData().get(getData().size() - 1)).getId();
            }
            List<HomeEntity> list = (List<HomeEntity>) listData;
            if (list != null) {
                lastListSize = list.size();
            }
        }
    };


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Dart.inject(this, getArguments());
        onStartLoading();
    }

    @Override
    public void onResume() {
        super.onResume();
        mlastid = 0;
//        getData().clear();
//        onStartLoading();
    }

    @Override
    public RecyclerViewBaseAdapter createAdapter() {
        return new HomeAdapter(getContext());
    }

    @Override
    public void onStartLoading() {
        getListData();
    }

    @Override
    public void onStartRefreshing() {
        super.onStartRefreshing();
        mlastid = 0;
        getData().clear();
       startLoading();
    }

    /**
     * 列表请求
     */
    public void getListData() {
        IHttpRequest request = new MyHttpRequest(getContext());
        UrlParams params = new UrlParams();
        params.put("size", PAGE_SIZE + "");
        params.put("last_id", mlastid + "");
        params.put("channel_id", mChannelID + "");
        request.get(UrlCenter.CHANNEL_ARTICLE_LIST, params, mRequestListener);
    }
}
